package com.hanhai.zrb.mobile.controller;

import java.io.IOException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.zrb.api.biz.service.FeedbackService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.BaseUser;
import com.hanhai.zrb.model.user.Feedback;
import com.hanhai.zrb.model.user.User;

@LoginNeedless
@RestController
@RequestMapping("/feedbackApi")
public class FeedbackApiController {
	private final Logger logger = LoggerFactory.getLogger(FeedbackApiController.class);
	@Resource
	private FeedbackService feedbackService;
	@Resource
	private UserService userService;

	/**
	 * APP页面提交意见内容
	 * @param contact 联系方式
	 * @param contents  意见内容
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("insertDate")
	public BaseApiResult findAllProvince(String contact, String contents,
			HttpServletResponse response) throws IOException {
		Feedback record = new Feedback();
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		String name = null;
		if (null != baseUser) {
			name = baseUser.getName();
			contact = baseUser.getMobile();
		} else {
			User user = userService.findByPhone(contact);
			if (null != user) {
				name = user.getUname();
				contact = user.getUmobile();
			}
		}
		record.setContents(contents);
		record.setNames(name);
		record.setCtime(new Date());
		record.setContact(contact);
		record.setSubType("APP");
		record.setStatus(0);
		BaseApiResult result = new BaseApiResult();
		try {
			feedbackService.insert(record);
			result.setMsg("意见反馈成功");
			result.setRes(1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("do  FeedbackApiController.findAllProvince exception", e);
			result.setMsg("意见反馈失败");
			result.setRes(0);
		}

		return result;

	}

}
